/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
import java.util.Stack;
public class Solution {
    public ListNode ReverseList(ListNode head) {
        Stack<ListNode> stack=new Stack<>();
        //把链表节点全部摘掉放入栈中
        while(head!=null){
            stack.push(head);
            head=head.next;
        }
        if(stack.isEmpty()){
            return null;
        }
        ListNode node=stack.pop();
        ListNode dummy=node;
        //栈中的节点全部出栈
        while(!stack.isEmpty()){
            ListNode tempNode=stack.pop();
            node=node.next;
        }
        node.next=null;
        return dummy;
    }
}